package common.dao.gremlin;

import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Client;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import common.helper.toolkit.FileToolkit;

/**
 * Created by julianzliu on 2016/7/3.
 */
@Component
public class GremlinGraphDao {
    /*
    Using gremlin java-driver
    To Submit gremlin(based on groovy) script....
    Like Using Java To Submit SQL script
     */

    final Logger logger = LoggerFactory.getLogger(GremlinGraphDao.class);

    private final String gremlin_server_conf = "/gremlin_admin/gremlin_servers.yml";

    private Cluster cluster;
    private Client client;

    //@Inject
    public GremlinGraphDao( ){
        logger.info("[GremlinGraphDao] nothing to do!");
        //startDao();
    }

    public GremlinGraphDao( String mode){
        if(mode.equalsIgnoreCase("debug")){
            logger.info("[GremlinGraphDao] nothing to do!");
            //startDao();
        }
        else{
            logger.info("[GremlinGraphDao] nothing to do!");
            //System.out.print("do nothing!");
        }
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        stopDao();
    }

    private void stopDao(){
        this.client.close();
        this.cluster.close();
        logger.debug("[close connection] to gremlin server");
    }



    private void startDao(){
        /*
        1) for Tinkerpop 3.2
         */
        /*
        http://www.programcreek.com/java-api-examples/org.apache.commons.configuration.PropertiesConfiguration
         */
        /*
        Configuration conf =  new PropertiesConfiguration();
        conf.addProperty("hosts" , "192.168.1.107");
        conf.addProperty("port" ,  8182);
        */
        /*
        2) for 3.0.1-incubating
        http://tinkerpop.apache.org/docs/3.0.1-incubating/#_tinkerpop3
         */
        try {
            /*
            打包jar后无法定位文件！！！
             */
            String gremlin_severs_path = FileToolkit.getFullPathInResource(gremlin_server_conf);
            this.cluster = Cluster.open(gremlin_severs_path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.client = cluster.connect();
        logger.debug("[open connection] to gremlin server");
    }






    public static void main(String[] args){
        GremlinGraphDao dao = new GremlinGraphDao("debug");
        //ResultSet results = dao.client.submit("g.V()");
        //String gremlin = "g.V()";
        String gremlin = "g1 = TinkerFactory.createModern().traversal()";
        dao.client.submit(gremlin);
        /*
        gremlin = "g.V()";
        ResultSet results = dao.client.submit(gremlin);
        Iterator it = results.iterator();
        while(it.hasNext()){
            Result rt = (Result)it.next();
            System.out.println( rt );
        }
        */
        System.out.println("Execute Gremlin Successful!!!");

    }
}
